import numpy as np
import time
import cv2 as cv

index_list = [[5, 492, 260], [6, 411, 218], [7, 354, 185],[8,411,218]] #初始化index_list

def generate_pic(local_list):
    n, width, height = local_list[0], int(local_list[1] / 3), local_list[2]+4
    # 判断能有多少组0-8色,width*（height/3）必须是8的倍数
    color_list = [[1,1,1],[0,0,1],[0,1,0],[1,0,0],[0,1,1],[1,0,1],[1,1,0],[0,0,0]]
    how_many_color = int((int(height/3)*width)/len(color_list))
    del_color_num = len(color_list) - (int(height/3)*width)%len(color_list)
    
    if del_color_num == 0:
        all_color_list = color_list * how_many_color
    else:
        all_color_list = color_list * (how_many_color+1)
        all_color_list = all_color_list[:-del_color_num]

    all_color_list = np.array(all_color_list)
    all_color_list = all_color_list.reshape(-1,width,3)
    # 添加黑色包围
    black_margin_top = np.zeros((1,all_color_list.shape[1],3),dtype="uint8")
    all_color_list = np.concatenate((black_margin_top,all_color_list,black_margin_top),axis=0)
    black_margin_left = np.zeros((all_color_list.shape[0],1,3),dtype="uint8")
    all_color_list = np.concatenate((black_margin_left,all_color_list,black_margin_left),axis=1)
    # 添加白色包围
    white_margin_top = np.ones((1,all_color_list.shape[1],3),dtype="uint8")
    all_color_list = np.concatenate((white_margin_top,all_color_list,white_margin_top),axis=0)
    white_margin_left = np.ones((all_color_list.shape[0],1,3),dtype="uint8")
    all_color_list = np.concatenate((white_margin_left,all_color_list,white_margin_left),axis=1)
    # 把0，1映射到0，255
    all_color_list = all_color_list * 255
    # 存储单张的伪二维码
    cv.imwrite("./1.bmp", all_color_list)
    
    # 三个相同的叠加在一起
    all_color_list = np.concatenate((all_color_list,all_color_list,all_color_list),axis=0)
    # 存储三个伪二维码加在一起的伪二维码
    cv.imwrite("./3.bmp", all_color_list)
    h, w, c = all_color_list.shape
    local_binary_data1 = np.tile(all_color_list, (n, n))  # 宽高都复制n遍
    # reshape成合理的宽高
    local_binary_data1 = local_binary_data1.reshape(-1, w * n, 3)
    cv.imwrite("./n3.bmp",
                   local_binary_data1 * 255)  # 存为图像

    # 9个相同的叠加在一起
    all_color_list = np.concatenate((all_color_list,all_color_list,all_color_list),axis=1)
    # 存储9个伪二维码加在一起的伪二维码
    cv.imwrite("./9.bmp", all_color_list)
    h, w, c = all_color_list.shape
    local_binary_data1 = np.tile(all_color_list, (n, n))  # 宽高都复制n遍
    # reshape成合理的宽高
    local_binary_data1 = local_binary_data1.reshape(-1, w * n, 3)
    cv.imwrite("./n9.bmp",
                   local_binary_data1 * 255)  # 存为图像
    print(all_color_list.shape)
    print(n, width, height)

    pic_name = "./9.bmp"
    show_pic(pic_name, n=6, wait_key=10000)

def show_pic(pic_name, n=6, wait_key=10000):
    """
    desc: 将生成的图像挨个展示, 按10帧率的频率进行播放图像, 即1000/10
    param:
    pic_name : 文件路径
    n: 图像从bmp*n倍显示
    wait_key: 一张图片显示在屏幕的时间
    return:
        NONE
    """
    image_data = cv.imread(pic_name)
    h, w, c = image_data.shape
    resize_width = int(w * n)
    resize_height = int(h * n)
    windowname = "Image"    # 使用中文会乱码
    cv.namedWindow(windowname, cv.WINDOW_NORMAL)
    cv.resizeWindow(windowname, resize_width, resize_height)
    cv.imshow(windowname, image_data)
    wait_key=20000
    cv.waitKey(wait_key)
    # cv.destroyAllWindows()
    return None

def main():
    local_list = index_list[2]
    generate_pic(local_list)


if __name__ == "__main__":
    main()

